# 请求封装
from conftest import *
import requests
from common.logger import logger
import os
from .config import Config

# 该类直接把token设置在请求头中了，后面需要认证的可以调这个类
from .yaml_util import read_taken


class RequestUtil:

    def __init__(self):
        self.session = requests.Session()
        # self.token = os.environ.get("API_TOKEN") # 读取环境变量
        self .token = read_taken('/test_data/config.yaml', "t")
        self.session.headers = {"Authorization": f"Bearer {self.token}"}
        # self.base_url1 = base_url
        print(self.token)


    def send_request(self, method, url, **kwargs):
        """
        发送 HTTP 请求
        :param method: GET/POST/PUT/DELETE
        :param url: 请求地址
        :param kwargs: headers/params/data/json/files
        :return: 响应对象
        """
        config = Config('config.ini')
        base_url2 = config.get_base_url()
        print(base_url2,'测试')
        url = base_url2 + url
        method = method.upper()
        logger.info(f"请求方式: {method}, 请求URL: {url}, 请求参数: {kwargs}")
        print(type(url))

        try:
            response = self.session.request(method, url, **kwargs)
            logger.info(f"响应状态码: {response.status_code}, 响应结果: {response.text}")
            return response
        except Exception as e:
            logger.error(f"请求异常: {e}")
            raise